<?php
/**
 * DeductModel.class.php
 * 用户分成模型
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-08-29
 */

namespace Common\Model;

class DeductModel extends BaseModel {

    protected $trueTableName    = 't_deduct';
    protected $cache    = true;

    /**
     * 字段处理
     * @param   $data
     * @param   $batch
     * @return  $data
     */
    static function resolveColumn($data, $batch = false){
        $resolveColumn   = function(&$item){
            $item['goods_type_text']    = D('Money')::GOODS_TYPE_MAP[$item['goods_type']];
            $item['role_text']  = D('User')::ROLE_MAP[$item['role_id']];
            $item['create_date']    = date('Y-m-d H:i:s', $item['create_time']);
        };

        if($batch){
            foreach($data as &$item){
                $resolveColumn($item);
            }
        }else{
            $resolveColumn($data);
        }

        return $data;
    }

    /**
     * 设置查询条件
     * @param   $condtion
     * @param   $tbName
     * @return  $where
     */
    function setCondition($condition, $tbName = ''){
        $where  = parent::setCondition($condition, $tbName);
        $tbName     = $tbName ? $tbName.'.' : '';

        //查询时间设置
        if($condition['date_start'] && $condition['date_start'] != ''){
            $where[$tbName.'create_time'][]     = ['egt', strtotime($condition['date_start'])];
        }
        if($condition['date_end'] && $condition['date_end'] != ''){
            $where[$tbName.'create_time'][]     = ['lt', strtotime($condition['date_end'])];
        }

        return $where;
    }

    /**
     * 获取课程分成列表
     * @param   $condition
     * @param   $page
     * @param   &$pageHtml
     * @param   &$count [<description>]
     * @return  $list
     */
    function getCourseList($condition, $page, &$pageHtml = false, &$count = false){
        $where  = array_merge(
            $this->setCondition($condition, 'a'),
            D('Money')->setCondition([
                'goods_type'    => 1
            ], 'b'),
            D('User')->setCondition([
                'keyword'   => $condition['keyword'],
                'status'    => false
            ], 'c')
        );

        $query  = $this->table('t_deduct a')
            ->join('left join t_money b on a.money_id = b.id')
            ->join('left join t_user c on b.user_id = c.id')
            ->join('left join t_course d on b.goods_id = d.id')
            ->where($where)
            ->cache($this->cache);

        if($count === true){
            $count  = (clone $query)->count();
        }

        if($pageHtml === true){
            $Page   = new \Think\Page((clone $query)->count(), C('PAGE_COUNT'));
            $pageHtml   = $Page->angularShow();
        }

        $list   = $query->page($page, C('PAGE_COUNT'))
            ->field('a.*, c.nickname, c.truename, c.role_id, d.name course_name, b.amount total_amount, b.goods_type, d.name course_name')
            ->select();
        return self::resolveColumn($list, true);
    }

    /**
     * 获取购物车分成列表
     * @param   $condition
     * @param   $page
     * @param   &$pageHtml
     * @param   &$count 
     * @return  $list
     */
    function getShopCartList($condition, $page, &$pageHtml = false, &$count = false){
        $where  = array_merge(
            $this->setCondition($condition, 'a'),
            D('Money')->setCondition([
                'goods_type'    => 4
            ], 'b'),
            D('User')->setCondition([
                'keyword'   => $condition['keyword'],
                'status'    => false
            ], 'c')
        );

        $query  = $this->table('t_deduct a')
            ->join('left join t_money b on a.money_id = b.id')
            ->join('left join t_user c on b.user_id = c.id')
            ->join('left join t_shop_cart d on b.goods_id = d.id')
            ->where($where)
            ->cache($this->cache);

        if($count === true){
            $count  = (clone $query)->count();
        }

        if($page === true){
            $Page   = new \Think\Page((clone $query)->count(), C('PAGE_COUNT'));
            $pageHtml   = $Page->angularShow();
        }

        $list   = $query->page($page, C('PAGE_COUNT'))
            ->field('a.*, c.truename, c.nickname, d.goods_infos, b.amount total_amount, b.goods_type')
            ->select();

        return self::resolveColumn($list, true);
    }

    /**
     * 获取VIP售出分成列表
     * @param   $condition
     * @param   $page
     * @param   &$pageHtml
     * @param   &$count
     * @return  $list
     */
    function getVipList($condition, $page, &$pageHtml = false, &$count = false){
        $where  = array_merge(
            $this->setCondition($condition, 'a'),
            D('Money')->setCondition([
                'goods_type'    => 5
            ], 'b'),
            D('User')->setCondition([
                'keyword'   => $condition['keyword'],
                'status'    => false
            ], 'c')
        );

        $query  = $this->table('t_deduct a')    
            ->join('left join t_money b on a.money_id = b.id')
            ->join('left join t_user c on b.user_id = c.id')
            ->where($where)
            ->cache($this->cache);

        if($count === true){
            $couont     = (clone $query)->count();
        }

        if($pageHtml === true){
            $Page   = new \Think\Page((clone $query)->count(), C('PAGE_COUNT'));
            $pageHtml   = $Page->angularShow();
        }

        //分页为false，不执行查询
        if($page === false){
            return null;
        }

        $list   = $query->page($page, C('PAGE_COUNT'))
            ->field('a.*, c.truename, c.nickname, b.amount total_amount, b.goods_type')
            ->select();
        return self::resolveColumn($list, true);
    }
}